package com.cn.xiaonuo.tcrfinance.tcrshops.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.xiaonuo.common.entity.BusinessMessage;
import com.cn.xiaonuo.common.utils.ApiJsonModel;
import com.cn.xiaonuo.common.utils.GetJavaTypeConvert;
import com.cn.xiaonuo.core.pojo.response.ResponseData;
import com.cn.xiaonuo.core.pojo.response.SuccessResponseData;
import com.cn.xiaonuo.tcrfinance.tcrorder.entity.PvTicketingRecord;
import com.cn.xiaonuo.tcrfinance.tcrshops.entity.PvProductGl;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.HtProductListParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.ProductReqParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.PvProductParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.param.TicketParam;
import com.cn.xiaonuo.tcrfinance.tcrshops.service.MerchantBackService;
import com.cn.xiaonuo.tcrfinance.tcrshops.service.ProductService;
import com.cn.xiaonuo.tcrfinance.tcrusers.param.MasterAccountExcelParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * banner图
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("")
public class ProductController {
    @Autowired
    private ProductService productService;
    @Autowired
    private MerchantBackService merchantBackService;
    /**
     * 查询商品信息
     */
    @ApiOperation(value = "商品分页查询", notes = "商品分页查询")
    @PostMapping("/api/product/selectProductInfo")
    public BusinessMessage selectProductInfoDetail(ProductReqParam productReqParam) {
        BusinessMessage message = merchantBackService.selectProductInfoDetail(productReqParam);
        return message;
    }
    /**
     * 更新商品虚拟销量
     */
    @ApiOperation(value = "更新商品虚拟销量", notes = "更新商品虚拟销量")
    @PostMapping("/api/product/updateProductXuNiVolume")
    public BusinessMessage updateProductXuNiVolume(@RequestBody PvProductParam pvProductParam){
        BusinessMessage message = productService.updateProductXuNiVolume(pvProductParam);
        return message;
    }

    /**
     *上架，下架，状态
     * @param productReqParam
     * @return
     */
    @ApiJsonModel({
            @ApiModelProperty(name = "productId", value = "商品id",example = "商品id",required = true),
            @ApiModelProperty(name = "status", value = "商品状态",example = "商品状态"),
    })
    @ApiOperation(value = "上架，下架状态", notes = "上架，下架状态")
    @PostMapping("/api/product/updateProductStatus")
    public BusinessMessage updateProductStatus(@RequestBody ProductReqParam productReqParam){
        BusinessMessage message=productService.updateProductStatus(productReqParam);
        return message;
    }
    /**
     * 添加商品
     *
     */
    /**
     * 编辑商品信息
     *
     * @number 70
     */
    @ApiJsonModel({
            @ApiModelProperty(name = "count", value = "商品数量",example = "商品数量"),
            @ApiModelProperty(name = "detail", value = "商品详情",example = "商品详情"),
            @ApiModelProperty(name = "imageUrl", value = "图片地址",example = "图片地址"),
            @ApiModelProperty(name = "name", value = "商品名",example = "商品名",required = true),
            @ApiModelProperty(name = "osort", value = "商品排序",example = "商品排序"),
            @ApiModelProperty(name = "personal_price", value = "免税价格",example = "免税价格"),
            @ApiModelProperty(name = "price", value = "商品价格",example = "商品价格"),
            @ApiModelProperty(name = "product_type_id", value = "商品类型id",example = "商品类型id"),
            @ApiModelProperty(name = "sales_mode_id", value = "销售模式",example = "销售模式"),
            @ApiModelProperty(name = "shopId", value = "店铺ID",example = "店铺ID"),
            @ApiModelProperty(name = "unlockday", value = "STC特殊商品比例",example = "STC特殊商品比例"),
    })
    @ApiOperation(value = "添加商品", notes = "添加商品")
    @PostMapping("/api/product/insertProductInfo")
    public BusinessMessage insertProductInfo(@RequestBody PvProductParam pvProductParam) {
        BusinessMessage message = merchantBackService.insertProductInfo(pvProductParam);
        return message;
    }

    /**
     * 编辑商品信息
     *
     * @number 70
     */
    @ApiJsonModel({
            @ApiModelProperty(name = "id", value = "商品id",example = "商品id",required = true),
            @ApiModelProperty(name = "count", value = "商品数量",example = "商品数量"),
            @ApiModelProperty(name = "detail", value = "商品详情",example = "商品详情"),
            @ApiModelProperty(name = "imageUrl", value = "图片地址",example = "图片地址"),
            @ApiModelProperty(name = "name", value = "商品名",example = "商品名"),
            @ApiModelProperty(name = "osort", value = "商品排序",example = "商品排序"),
            @ApiModelProperty(name = "personal_price", value = "免税价格",example = "免税价格"),
            @ApiModelProperty(name = "price", value = "商品价格",example = "商品价格"),
            @ApiModelProperty(name = "product_type_id", value = "商品类型id",example = "商品类型id"),
            @ApiModelProperty(name = "sales_mode_id", value = "销售模式",example = "销售模式"),
            @ApiModelProperty(name = "shopId", value = "店铺ID",example = "店铺ID"),
            @ApiModelProperty(name = "unlockday", value = "STC特殊商品比例",example = "STC特殊商品比例"),
    })
    @ApiOperation(value = "修改商品", notes = "修改商品")
    @PostMapping("/api/product/updateProductInfo")
    public BusinessMessage updateProductInfo(@RequestBody PvProductParam pvProductParam) {
        BusinessMessage message = merchantBackService.updateProductInfo(pvProductParam);
        return message;
    }

    /**
     * 商品上票
     */
    @ApiOperation(value = "获取商品上票信息", notes = "获取商品上票信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id", paramType = "form"),
    })
    @PostMapping("/api/product/queryTicket")
    public BusinessMessage queryTicket(Integer productId){
        BusinessMessage<Object> message = new BusinessMessage<>();
        PvProductGl pvProductGl = productService.queryTicket(productId);
        if(null==pvProductGl){
            message.setCode("-1");
            message.setSuccess(false);
        }
        message.setData(pvProductGl);
        message.setCode("200");
        message.setSuccess(true);
        return message;
    }
    @ApiOperation(value = "清空当日成交量", notes = "清空当日成交量")
    @PostMapping("/api/product/clearVolume")
    public BusinessMessage clearVolume(){
        BusinessMessage<Object> message = new BusinessMessage<>();
        Integer volume = productService.clearVolume();
        message.setCode("200");
        message.setMessage("操作成功");
        message.setData(volume);
        message.setSuccess(true);
        return message;
    }

    @PostMapping("/api/product/detail")
    @ApiJsonModel({
            @ApiModelProperty(name = "id", value = "商品id",example = "商品id",required = true),
    })
    @ApiOperation(value = "商品_查看", notes = "商品_查看")
    public ResponseData detail(@RequestBody PvProductParam productParam) {
        return new SuccessResponseData(productService.detail(productParam));
    }

    /**
     * 查询商品名字
     */
    @ApiOperation(value = "和平村寄售商品_列表", notes = "和平村寄售商品_列表")
    @PostMapping("/api/product/queryProductNameList")
    public BusinessMessage queryProductNameList(@RequestBody ProductReqParam productReqParam){
        BusinessMessage message = productService.queryProductNameList(productReqParam);
        return message;
    }
    /**
      查询用户拥有的商品的信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id",example = "商品id"),
            @ApiImplicitParam(name = "productName", value = "商品名称",example = "商品名称"),
            @ApiImplicitParam(name = "phone", value = "手机号码",example = "手机号码"),
            @ApiImplicitParam(name = "productId", value = "商品id",example = "商品id"),
            @ApiImplicitParam(name = "pageNo", value = "操作员",example = "操作员"),
            @ApiImplicitParam(name = "pageNo", value = "主键",example = "主键")
    })
    @PostMapping("/api/product/queryProductNumByUserId")
    public BusinessMessage queryProductNumByUserId(@RequestBody Map<String,Object> params){
        BusinessMessage<Object> message = new BusinessMessage<>();
        Integer pageNo = GetJavaTypeConvert.getInteger(params.get("pageNo"));
        Integer pageSize = GetJavaTypeConvert.getInteger(params.get("pageSize"));
        if (null == pageNo || pageNo <= 1) {
            pageNo=1;
        }
        if (null == pageSize || pageSize <= 1) {
            pageSize=10;
        }
        Integer productId = GetJavaTypeConvert.getInteger(params.get("productId"));
        String productName = GetJavaTypeConvert.getString(params.get("productName"));
        String accounts = GetJavaTypeConvert.getString(params.get("accounts"));
        String phone = GetJavaTypeConvert.getString(params.get("phone"));
        // 设置分页参数
        PageHelper.startPage(pageNo, pageSize);
        List<Map<String, Object>> resultList = productService.queryProductNumByUserId(productId, productName, accounts, phone);
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(resultList);
        if(null!=resultList && resultList.size()>=0){
            message.setMessage("查询成功");
            message.setSuccess(true);
            message.setCode("200");
            message.setData(pageInfo);
            return message;
        }
        message.setMessage("查询失败");
        message.setSuccess(false);
        message.setCode("-1");
        message.setData(pageInfo);
        return message;

    }

    @ApiOperation(value = "更改商品售卖状态", notes = "更改商品售卖状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id",example = "商品id"),
            @ApiImplicitParam(name = "isSale", value = "售卖状态 1:正常 2：禁用",example = "1"),
    })
    @PostMapping("/api/product/updateSalesStatus")
    public BusinessMessage updateSalesStatus(@RequestParam(value = "productId") Integer productId, @RequestParam(value = "isSale")Integer isSale) {
        BusinessMessage message = productService.updateSalesStatus(productId,isSale);
        return message;
    }

    @ApiOperation(value = "乡村振兴上票记录", notes = "上票记录")
    @PostMapping("/api/product/tipketPage")
    public Page<PvTicketingRecord> ticketPage(@RequestBody TicketParam param) {
        return productService.ticketPage(param);

    }

    @ApiOperation(value = "乡村振兴一键摘单商品列表", notes = "一键摘单商品列表")
    @ApiJsonModel({
            @ApiModelProperty(name = "pickProductId", value = "商品id",example = "商品id（true）",required = true),
            @ApiModelProperty(name = "pageSize", value = "每页多少数量",example = "每页多少数量（true）",required = true),
            @ApiModelProperty(name = "pageNum", value = "页码",example = "页码（true）",required = true)
    })
    @RequestMapping(value = "/api/product/queryHpcHtProductList", method = RequestMethod.POST)
    public BusinessMessage queryHpcHtProductList(@RequestBody HtProductListParam param){
        BusinessMessage message = productService.queryHpcHtProductList(param);
        return message;
    }

    @ApiOperation(value = "交易中心--促销商品摘单记录按月导出", notes = "促销商品摘单记录按月导出")
    @RequestMapping(value = "/api/product/exportPickingRecords", method = RequestMethod.GET)
    public void exportPickingRecords( HttpServletResponse response, MasterAccountExcelParam param){
        productService.exportPickingRecords(response,param);
    }
}
